package com.lihan.services.impl;

import com.lihan.enums.MsgActionEnum;
import com.lihan.enums.SearchFriendsStatusEnum;
import com.lihan.mapper.FriendsRequestMapper;
import com.lihan.mapper.MyFriendsMapper;
import com.lihan.mapper.UserMapperCustom;
import com.lihan.model.entity.FriendsRequest;
import com.lihan.model.entity.MyFriends;
import com.lihan.model.entity.User;
import com.lihan.model.vo.FriendsRequestVo;
import com.lihan.model.vo.MyFriendsVo;
import com.lihan.netty.DataContent;
import com.lihan.netty.UserChannelRel;
import com.lihan.services.IFriendService;
import com.lihan.services.IUserService;
import com.lihan.utils.JsonUtils;
import io.netty.channel.Channel;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import org.n3r.idworker.Sid;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ClassName: IFriendServiceImpl
 * Package: com.lihan.services.impl
 * Description:
 *
 * @Author 李涵
 * @Create 2025-05-08 17:27
 * @Version 1.0
 */
@Service
public class FriendServiceImpl implements IFriendService {

    @Resource
    private IUserService userService;

    @Resource
    private MyFriendsMapper friendsMapper;

    @Resource
    private FriendsRequestMapper friendRequestMapper;

    @Resource
    private UserMapperCustom userMapperCustom;

    @Resource
    private MyFriendsMapper myFriendsMapper;

    @Resource
    Sid sid;

    @Override
    public Integer preconditionSearchFriend(String myUserId, String friendUserName) {

        User user = userService.getUserByUsername(friendUserName);
        // 1.如果用户不存在，返回1
        if(user == null){
            return SearchFriendsStatusEnum.USER_NOT_EXIST.status;
        }
        // 2.如果用户是你自己，返回2
        if(myUserId.equals(user.getId())){
            return SearchFriendsStatusEnum.NOT_YOURSELF.status;
        }

        MyFriends myFriend = new MyFriends();
        myFriend.setMyUserId(myUserId);
        myFriend.setMyFriendUserId(user.getId());
        MyFriends myF = friendsMapper.selectOneByExample(myFriend);
        if(myF != null){
            return SearchFriendsStatusEnum.ALREADY_FRIENDS.status;
        }
        return SearchFriendsStatusEnum.SUCCESS.status;
    }



    @Override
    public void sendFriendRequest(String myUserId, String friendUserName) {
        // 根据用户名查询出该用户信息的用户id
        User user = userService.getUserByUsername(friendUserName);

        //存入好友请求表
        FriendsRequest friendsRequest = new FriendsRequest();
        friendsRequest.setId(sid.nextShort());
        friendsRequest.setSendUserId(myUserId);
        friendsRequest.setAcceptUserId(user.getId());
        friendsRequest.setRequestDateTime(new Date());
        friendRequestMapper.insert(friendsRequest);

    }

    @Override
    public List<FriendsRequestVo> queryFriendRequestList(String acceptUserId) {
        return userMapperCustom.queryFriendRequestList(acceptUserId);
    }

    @Override
    public void deleteFriendRequest(FriendsRequest friendsRequest) {
        friendRequestMapper.deleteFriendRequest(friendsRequest);
    }

    @Override
    public void passFriendRequest(String sendUserId, String acceptUserId) {
        savaFriends(sendUserId,acceptUserId);
        savaFriends(acceptUserId,sendUserId);

        Channel sendChannel = UserChannelRel.get(sendUserId);
        if(sendChannel!=null){
            //使用websocket主动推送消息到请求发起者，更新他的通讯录列表为最新
            DataContent dataContent = new DataContent();
            dataContent.setAction(MsgActionEnum.PULL_FRIEND.type);

            //消息的推送
            sendChannel.writeAndFlush(new TextWebSocketFrame(JsonUtils.objectToJson(dataContent)));
        }
    }

    //通过好友请求并保存数据到my——friends表中
    private void savaFriends(String sendUserId,String acceptUserId){
        MyFriends myFriends = new MyFriends();
        String recordId = sid.nextShort();
        myFriends.setId(recordId);
        myFriends.setMyUserId(sendUserId);
        myFriends.setMyFriendUserId(acceptUserId);

        myFriendsMapper.insert(myFriends);

    }

    @Override
    public List<MyFriendsVo> queryMyFriends(String userId) {
        return userMapperCustom.queryMyFriends(userId);
    }
}
